<?php
class Administrator_SeoerController extends Zend_Controller_Action
{
	public function init() {
		
	}
	
	public function indexAction(){
		$objSeoer = new HT_Model_administrator_models_seoer();
		$do = @$this->_request->getParam('do');
		$seoer_id = (int)$this->_request->getParam('id');
		if($do == 'delete' && $seoer_id >0){
			$this->deleteSeoer($seoer_id);
		}elseif($do == 'list'){
			$this->getListSeoer();
		}else{
			$keyword = $this->_request->getParam('keyword');
			$this->view->keyword 	= $keyword;
		}
		$this->view->inlineScript()->appendFile(WEB_PATH.'/application/modules/administrator/views/scripts/seoer/index.js');
	}
	
	public function updateAction(){
		$objSeoer 		= new HT_Model_administrator_models_seoer();
		$objUtil 		= new HT_Model_administrator_models_utility();
		$do 			= @$this->_request->getParam('do');
		$seoer_id 		= (int)$this->_request->getParam('id');
		$status 		= $this->_request->getParam('seoer_status');
		if($status == 'on'){
			$status 	= 1;
		}else{
			$status		= 0;
		}
		//echo $do; die();
		if($do == 'submit'){
			$data = array();
			$data['seoer_status'] 		= $status;
			$data['description'] 		= $this->_request->getParam('description');
			if($seoer_id >0){
				$objSeoer->update($data, 'seoer_id='.(int)$seoer_id);
			}else{
				$data['wishid'] 	=  $this->_request->getParam('wishid');
				$seoer_id 			= $objSeoer->addData($data);
			}
			$this->_redirect(WEB_PATH.'/administrator/seoer');
		}elseif($seoer_id >0){
			$this->view->seoer = $objSeoer->getSeoer($seoer_id);
		}
		
		$user 					= $objUtil->GetCombobox('wishid','wishid',"CONCAT(user_name,' - ',email,' - ',firstname,' ',lastname) as fullname",'user',array('isBlankVal'=>'Vui lòng chọn một thành viên trở thành nhân viên SEO.','defaultValue'=>$seoer_id,'orderBy'=>'email','valueColumnAlias'=>'fullname'));
		
		$this->view->user 		= $user;
		$this->view->id 		= $seoer_id;
		$this->view->inlineScript()->appendFile(WEB_PATH.'/application/modules/administrator/views/scripts/seoer/update.js');
	}

	function deleteSeoer($seoer_id){
		//$objSeoer = new HT_Model_administrator_models_seoer();
		//echo $objSeoer->delete("id=".(int)$seoer_id);die();
	}

	function getListSeoer(){
		$objUtil 		= new HT_Model_administrator_models_utility();
		$objSeoer 	= new HT_Model_administrator_models_seoer();
		$keyword 		= trim($this->_request->getParam('keyword'));
		$page 			= (int)$this->_request->getParam('page');
		$size 			= PAGING_SIZE;
		if (!is_numeric($page) || $page <= 0) {
			$page = 1;
		}
		$start = $page * $size - $size;
		$totalRecord = $objSeoer->getListSeoer_nb(array('keyword'=>$keyword));
		$listSeoer = $objSeoer->getListSeoer($start,$size,array('keyword'=>$keyword));
		$paging = trim($objUtil->paging($page, $size, $totalRecord));

		$ajaxData = null;
		if($paging){
			$ajaxData .= '<div class="paging_box">';
			$ajaxData .= $paging;
			$ajaxData .= '<div class="cb"></div></div>';
		}
		$ajaxData .= '<table cellspacing="0" class="tablesorter tablesorterBorder">';
		$ajaxData .= '<thead>';
			$ajaxData .= '<tr>';
				$ajaxData .= '<th width="15">STT</th>';
				$ajaxData .= '<th width="200">Họ Tên</th>';
				$ajaxData .= '<th width="200">Nickname</th>';
				$ajaxData .= '<th width="200">Email</th>';
				$ajaxData .= '<th width="200">Trạng thái</th>';
				$ajaxData .= '<th width="400">Mô tả về nhân viên</th>';
				$ajaxData .= '<th style="white-space: nowrap;padding-right: 5px;" align="center">Điều khiển</th>';
			$ajaxData .= '</tr>';
		$ajaxData .= '</thead>';
		
		$i=0;
		foreach($listSeoer as $seoer){
			$status = $seoer['seoer_status'];
			if($status){
				$status = "Đã kích hoạt";
			}else{
				$status = null;
			}
			$i++;
			$trClass = null;
			if($i%2 == 1) $trClass = ' class="altrow"';
			$ajaxData .= '<tr id="'.$seoer['seoer_id'].'" '.$trClass.'>';
			$ajaxData .= '<td align="center">'.$i.'</td>';
			$ajaxData .= '<td><a href="'.WEB_PATH.'/administrator/seoer/update/?id='.$seoer['seoer_id'].'">'.$objUtil->tooltipString($seoer['fullname'],200).'</a></td>';
			$ajaxData .= '<td>'.$seoer['user_name'].'</td>';
			$ajaxData .= '<td>'.$seoer['email'].'</td>';
			$ajaxData .= '<td>'.$status.'</td>';
			$ajaxData .= '<td>'.$seoer['description'].'</td>';
			$ajaxData .= '<td style="white-space: nowrap" align="center">';
			$ajaxData .= '<a href="'.WEB_PATH.'/administrator/seoer/update/?id='.$seoer['seoer_id'].'">Sửa</a>';
			$ajaxData .= '</td>';
			$ajaxData .= '</tr>';
		}
		$ajaxData .= '</tbody>';
		$ajaxData .= '</table>';
		$ajaxData .= '</div>';
		if($paging){
			$ajaxData .= '<div class="paging_box">';
			$ajaxData .= $paging;
			$ajaxData .= '<div class="cb"></div></div>';
		}
		echo $ajaxData; die();
	}
}
